﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Media;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.Charts.Navigation;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Microsoft.Research.DynamicDataDisplay.PointMarkers;
using Microsoft.Research.DynamicDataDisplay.ViewportRestrictions;
using treadmillProject.src.model;


namespace GUI.src {
    public class PulseGraph : IGraph {
        private ChartPlotter chartPlotter;
        private Pen myPen = new Pen();
        private PenDescription myPenDescription = new PenDescription();
        private CircleElementPointMarker myCircleElementPointMarker = new CircleElementPointMarker();
        private const int N = 100;
        private LineGraph lineGraph;
        private int xSize = 0;
        private int num = 0;


        public LinkedList<double> x = new LinkedList<double>();
        public LinkedList<double> y = new LinkedList<double>();

        private CompositeDataSource myCompositeDataSource;
        private List<TrainingData> trainingData = TrainingSession.trainingData;


        private LineAndMarker<ElementMarkerPointsGraph> lg;

        public PulseGraph(ChartPlotter chartPlotter) {
            this.chartPlotter = chartPlotter;
            Effects();

            Console.WriteLine("PulseGraph init...");
        }


        private void AddToPulseGraph() {
            ResetPulsGraph();
            for (int i = 0; i < trainingData.Count(); i++) {
                y.AddLast(trainingData[i].pulse.PulseData);
                x.AddLast(i);
            }
        }


        public void ResetPulsGraph() {
            x.Clear();
            y.Clear();
            xSize = 0;
        }


        public void Draw() {
            // Create data sources

            var yDataSource = new EnumerableDataSource<double>(y);
            yDataSource.SetYMapping(Y => Y);

            var xDataSource = new EnumerableDataSource<double>(x);
            xDataSource.SetXMapping(X => X);

            yDataSource.AddMapping(ShapeElementPointMarker.ToolTipTextProperty,
                                   Y => String.Format("Value is {0}", Y));

            myCompositeDataSource = new CompositeDataSource(xDataSource, yDataSource);

            chartPlotter.Children.Remove(lineGraph);
            //lineGraph = chartPlotter.AddLineGraph(myCompositeDataSource);
            lineGraph = chartPlotter.AddLineGraph(myCompositeDataSource, myPen, myPenDescription);

            // Force evertyhing plotted to be visible
            chartPlotter.FitToView();
        }

        private void Effects() {
            myPen.Brush = Brushes.Red;
            myPen.Thickness = 2;
            myCircleElementPointMarker.Size = 6;
            myCircleElementPointMarker.Fill = Brushes.DarkRed;
            myPenDescription.DescriptionString = "Pulse Graph";
        }

        public void OnUpdate() {
            trainingData = TrainingSession.trainingData;
            AddToPulseGraph();
            Draw();
        }
    }
}